﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using HelloLLLLL.Blog.Common;
using HelloLLLLL.Blog.Models;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.IO;
using HelloLLLLL.Blog.DAL;

using System.Text;
using System.Data;

namespace HelloLLLLL.Blog.Admin
{
    /// <summary>
    /// AdminPostData 的摘要说明
    /// </summary>
    public class AdminPostData : IHttpHandler, IRequiresSessionState
    {
        DBHelper helper = new DBHelper();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";

            string action = context.Request.QueryString["action"];
            if (string.Compare(action, "adminLogin", false) == 0)
            {
                int varifyCode = 0;
                if (int.TryParse(context.Request["captcha"], out varifyCode))
                {
                    string json = string.Empty;
                    Debug.Write(varifyCode);
                    int sCode = -1;
                    var o = context.Session["varifyCode"];
                    if (o == null)
                    {
                        json = JsonConvert.SerializeObject(new { IsOk = "NoOk", Msg = "请刷新验证码再试" });
                        context.Response.Write(json);
                        return;
                    }
                    sCode = int.Parse(o.ToString());

                    string nick = context.Request["username"];

                    string loginip =QQWryLocator.GetIPAddress(context.Request);
                    if (loginip == "::1")
                    {
                        loginip = "127.0.0.1";
                    }

                    string result = HelloLLLLL.Blog.Common.QQWryLocator.GetstringIpAddress(loginip);
                    LLIP ipInfo = null;
                    if (!string.IsNullOrWhiteSpace(result) && result != string.Empty)
                    {
                        ipInfo = JsonConvert.DeserializeObject<LLIP>(result);
                    }
                    IPLocation ip = null;
                    string location = "";
                    if (ipInfo == null)
                    {
                        QQWryLocator qqsry = new QQWryLocator(context.Server.MapPath("/QQwry.Dat")); //http://www.cz88.net/下载
                        ip = qqsry.Query(loginip);

                    }

                    else
                    {
                        location = ipInfo.country + " " + ipInfo.city;
                    }
                    if (ip != null)
                    {
                        location = ip.Country;
                    }
                    if (string.IsNullOrWhiteSpace(location))
                    {

                        QQWryLocator qqsry = new QQWryLocator(context.Server.MapPath("/QQwry.Dat")); //http://www.cz88.net/下载
                        ip = qqsry.Query(loginip);
                        location = ip.Country;
                    }
                    if (string.IsNullOrWhiteSpace(location))
                    {
                        location = "未知地址";
                    }
                    LoginInfo lInfo = new LoginInfo();

                    lInfo.City = location;

                    lInfo.Ip = loginip;
                    lInfo.LoginDate = DateTime.Now.ToString();
                    lInfo.UserName = nick;

                    lInfo.UserOS = GetOs.GetOSVersion(context.Request.UserAgent);

                    string sqlLoginInfo = SqlBuilderHelper.InsertSql<LoginInfo>(lInfo, "LoginInfo");

                    helper.GetNonQuery(sqlLoginInfo);

                    if (sCode == varifyCode)
                    {

                        string pwd = context.Request["password"];
                        string sql1 = "select Id from UserInfo where Nick='{0}'";
                        sql1 = string.Format(sql1, nick);
                        string sql2 = "select * from UserInfo where Nick='{0}' and Password='{1}'";
                        int Id = Convert.ToInt32(helper.GetScalar(sql1, null));
                        List<UserInfo> list = null;
                        if (Id > 0)
                        {
                            string encrypt = MD5Encrypt.Encrypt(pwd, 32);

                            sql2 = string.Format(sql2, nick, encrypt);

                            list = new ModelHandler<UserInfo>().FillModel(helper.GetDataTable(sql2, null));
                        }

                        if (null != list && list.Count > 0)
                        {
                            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, list[0].Nick, DateTime.Now, DateTime.Now.AddDays(1), false, "password", "/");
                            string authticket = FormsAuthentication.Encrypt(ticket);
                            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, authticket);
                            cookie.Secure = false;
                            cookie.Expires = ticket.Expiration;
                            cookie.Path = FormsAuthentication.FormsCookiePath;
                            context.Response.Cookies.Add(cookie);

                            context.Session["userInfo"] = list[0];

                            json = JsonConvert.SerializeObject(new { IsOk = "Ok" });
                            context.Response.Write(json);
                        }

                        else
                        {
                            json = JsonConvert.SerializeObject(new { IsOk = "NoOk", Msg = "请重新登录" });
                            context.Response.Write(json);
                        }
                    }
                    else
                    {
                        json = JsonConvert.SerializeObject(new { IsOk = "NoOk", Msg = "验证码输入有误，请刷新验证码" });
                        context.Response.Write(json);
                        return;
                    }
                }
                else
                {
                    string json = JsonConvert.SerializeObject(new { IsOk = "NoOk", Msg = "" });
                    context.Response.Write(json);
                    return;
                }
            }
            else if (string.Compare(action, "yzm", false) == 0)
            {
                ValidateCode c = new ValidateCode();
                string code = c.CreateValidateCode(4);
                context.Session["varifyCode"] = code;

                c.CreateValidateGraphic(code, context);

            }
            else if (string.Compare(action, "loginInfo", false) == 0)
            {
                context.Response.ContentType = "application/json";
                LoginInfoData data = new LoginInfoData();

                int page = int.Parse(context.Request["page"]);
                int limit = int.Parse(context.Request["limit"]);


                data.code = 0;
                data.msg = "ok";
                var tb = helper.GetPageList("LoginInfo", "Id", (page - 1) * limit+1, page * limit);
                var count = int.Parse(helper.GetScalar("select count(1) from LoginInfo", null).ToString());
                List<LoginInfo> list = new ModelHandler<LoginInfo>().FillModel(tb);
                data.data = list;
                data.count = count;

                context.Response.Write(JsonConvert.SerializeObject(data));
            }
            else if (string.Compare(action, "notice", false) == 0)
            {
                context.Response.ContentType = "application/json";
                string p = context.Request["page"];
                string l = context.Request["limit"];
                int page = 1;
                if (!string.IsNullOrEmpty(p))
                    page = int.Parse(p);
                int limit = 10;
                if (!string.IsNullOrEmpty(l))
                    limit = int.Parse(l);

                NoticeData data = new NoticeData();
                data.code = 0;
                data.msg = "ok";

                string strTop = context.Request["top"];
                DataTable tb = null;
                int count = 5;
                if (strTop == null || strTop == "" || strTop == string.Empty)
                {
                    tb = helper.GetPageList("Notice", "Id", (page - 1) * limit + 1, page * limit);
                    count = int.Parse(helper.GetScalar("select count(1) from Notice", null).ToString());
                }
                else
                {
                    string sqlTop = "select top " + strTop + " *  from Notice order by id desc";

                    tb = helper.GetDataTable(sqlTop, null);
                     
                }
                data.data = new ModelHandler<Notice>().FillModel(tb);
                data.count = count;



                context.Response.Write(JsonConvert.SerializeObject(data));

            }
            else if (string.Compare(action, "article", false) == 0)
            {
                context.Response.ContentType = "application/json";
                string p = context.Request["page"];
                string l = context.Request["limit"];
                int page = -1;
                if (!string.IsNullOrEmpty(p))
                    page = int.Parse(p);
                int limit = -1;
                if (!string.IsNullOrEmpty(l))
                    limit = int.Parse(l);

                ArticleData data = new ArticleData();
                data.code = 0;
                data.msg = "ok";

                string strTop = context.Request["top"];
                DataTable tb=null;
                int count = 0;
                if (string.IsNullOrEmpty(strTop) || string.IsNullOrWhiteSpace(strTop))
                {

                    tb = helper.GetPageList("LLArticle", "Id", (page - 1) * limit + 1, page * limit);
                    count = int.Parse(helper.GetScalar("select count(1) from LLArticle", null).ToString());
                }
                else
                {
                    string sql = "select top " + strTop + " * from LLArticle order by Id desc";
                    tb = helper.GetDataTable(sql,null);
                    count = int.Parse(strTop);
                }
              
                data.data = new ModelHandler<LLArticle>().FillModel(tb);
                data.count = count;
                context.Response.Write(JsonConvert.SerializeObject(data));

            }
            else if (string.Compare(action, "llTimeLine", false) == 0)
            {
                context.Response.ContentType = "application/json";
                string p = context.Request["page"];
                string l = context.Request["limit"];
                int page = -1;
                if (!string.IsNullOrEmpty(p))
                    page = int.Parse(p);
                int limit = -1;
                if (!string.IsNullOrEmpty(l))
                    limit = int.Parse(l);

                LLTimeData data = new LLTimeData();
                data.code = 0;
                data.msg = "ok";
                var tb = helper.GetPageList(" LLTime", "Id", (page - 1) * limit + 1, page * limit);
                var count = int.Parse(helper.GetScalar("select count(1) from  LLTime", null).ToString());
                data.data = new ModelHandler<LLTime>().FillModel(tb);

                data.count = count;



                context.Response.Write(JsonConvert.SerializeObject(data));

            }
            else if (string.Compare(action, "getBannerList", false) == 0)
            {
                context.Response.ContentType = "application/json";
                List<Banner> listAr = new List<Banner>();
                string p = context.Request["page"];
                string l = context.Request["limit"];
                int page = 1;
                if (!string.IsNullOrEmpty(p))
                    page = int.Parse(p);
                int limit = 1;
                if (!string.IsNullOrEmpty(l))
                    limit = int.Parse(l);
                var tb = helper.GetPageList("Banner", "Id", (page - 1) * limit + 1, page * limit);

                listAr = new ModelHandler<Banner>().FillModel(tb);
                context.Response.Write(JsonConvert.SerializeObject(new { code = 1, data = listAr }));
                context.Response.End();
            }
            else if (string.Compare(action, "getCommentList", false) == 0)
            {
                context.Response.ContentType = "application/json";
                List<Pinglun> listAr = new List<Pinglun>();
                string p = context.Request["page"];
                string l = context.Request["limit"];
                int page = 1;
                if (!string.IsNullOrEmpty(p))
                    page = int.Parse(p);
                int limit = 6;
                if (!string.IsNullOrEmpty(l))
                    limit = int.Parse(l);
                var tb = helper.GetPageList("Pinglun", "Id", (page - 1) * limit + 1, page * limit);

                listAr = new ModelHandler<Pinglun>().FillModel(tb);
                context.Response.Write(JsonConvert.SerializeObject(new { code = 1, data = listAr }));
                context.Response.End();
            }
            else if (string.Compare(action, "articleType", false) == 0)
            {
                context.Response.ContentType = "application/json";
                string p = context.Request["page"];
                string l = context.Request["limit"];
                int page = -1;
                if (!string.IsNullOrEmpty(p))
                    page = int.Parse(p);
                int limit = -1;
                if (!string.IsNullOrEmpty(l))
                    limit = int.Parse(l);

                ArtTypeData data = new ArtTypeData();
                data.code = 0;
                data.msg = "ok";
                var tb = helper.GetPageList("[LLArtType]", "Id", (page - 1) * limit + 1, page * limit);
                var count = int.Parse(helper.GetScalar("select count(1) from [LLArtType]", null).ToString());
                data.data = new ModelHandler<LLType>().FillModel(tb);

                data.count = count;
                context.Response.Write(JsonConvert.SerializeObject(data));
            }
            //公告的图片上传
            else if (string.Compare(action, "uploadImg", false) == 0)
            {
                ImgJson iJson = new ImgJson();
                iJson.data = new List<string>();

                string dir = "\\UpImgs\\" + DateTime.Now.ToString("yyyyMMdd");
                string physicsPath = HttpContext.Current.Server.MapPath("~" + dir);
                if (!System.IO.Directory.Exists(physicsPath))
                {
                    System.IO.Directory.CreateDirectory(physicsPath);
                }
                for (int i = 0; i < context.Request.Files.Count; i++)
                {
                    string phPath = string.Empty;
                    //获取上传的文件的对象  
                    HttpPostedFile img = context.Request.Files[i];

                    //获取上传文件的名称  
                    string s = img.FileName;
                    var strFileExtension = s.Substring(s.LastIndexOf('.') + 1, s.Length - s.LastIndexOf('.') - 1);
                    string allowextension = System.Configuration.ConfigurationManager.AppSettings["ImageType"];
                    if (allowextension.ToLower().IndexOf(strFileExtension.ToLower()) >= 0)
                    {
                        int len = s.LastIndexOf(".") - s.LastIndexOf("\\") - 1;
                        string fileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Millisecond.ToString();
                        string fullName = fileName + "." + strFileExtension;

                        //fileName = fileName.Replace(",", "");
                        //fullName = fullName.Replace(",", "");
                        //fullName = fullName.Replace(":", "");
                        //截取获得上传文件的名称(ie上传会把绝对路径也连带上，这里只得到文件的名称)  
                        //string str = System.Guid.NewGuid().ToString("N")+ s.Substring(s.LastIndexOf(".") - 1); // s.Substring(s.LastIndexOf("\\") + 1);
                        string path = dir + "\\" + fullName;
                        phPath = physicsPath + "//" + fullName;
                        //保存文件  

                        if (allowextension.ToLower().IndexOf(strFileExtension.ToLower()) >= 0)
                        {
                            int width = int.Parse(context.Request["width"]);
                            int fileLen = img.ContentLength;
                            // 读取文件的 byte[] 
                            byte[] bytes = new byte[fileLen];

                            img.InputStream.Read(bytes, 0, fileLen);
                            MemoryStream ms = new MemoryStream(bytes);

                            ImageClass.MakeThumbnail(ms, phPath, width, 0, "W");
                        }
                        else
                        {
                            img.SaveAs(phPath);
                        }
                        iJson.data.Add(path);
                    }

                }
                if (iJson.data.Count > 0 && iJson.data.Count == context.Request.Files.Count)
                {
                    iJson.code = 1;
                }
                else
                {
                    iJson.code = 0;
                    iJson.msg = "上传图片出现错误";
                }
                context.Response.ContentType = "application/json";
                context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");
                context.Response.Write(JsonConvert.SerializeObject(iJson));
            }
            else if (string.Compare(action, "uploadImg1", false) == 0)
            {


                context.Response.AddHeader("Content-Type", "text/html; charset=UTF-8");

                string fileType = context.Request["dir"];
                string dir = string.Empty;
                string allowextension = string.Empty;
                if (fileType == "file")
                {
                    dir = "\\UpFiles\\" + DateTime.Now.ToString("yyyyMMdd");
                    allowextension = System.Configuration.ConfigurationManager.AppSettings["FileType"];
                }
                else if (fileType == "banner")
                {
                    dir = "\\banners\\" + DateTime.Now.ToString("yyyyMMdd");
                    allowextension = System.Configuration.ConfigurationManager.AppSettings["ImageType"];
                }
                else
                {
                    dir = "\\UpImgs\\" + DateTime.Now.ToString("yyyyMMdd");
                    allowextension = System.Configuration.ConfigurationManager.AppSettings["ImageType"];
                }
                try
                {

                    string physicsPath = HttpContext.Current.Server.MapPath("~" + dir);
                    if (!System.IO.Directory.Exists(physicsPath))
                    {
                        System.IO.Directory.CreateDirectory(physicsPath);
                    }
                    if (context.Request.Files.Count > 0)
                    {
                        string phPath = string.Empty;
                        //获取上传的文件的对象  
                        HttpPostedFile img = context.Request.Files[0];

                        //获取上传文件的名称  
                        string s = img.FileName;
                        var strFileExtension = s.Substring(s.LastIndexOf('.') + 1, s.Length - s.LastIndexOf('.') - 1);

                        if (allowextension.ToLower().IndexOf(strFileExtension.ToLower()) >= 0)
                        {
                            int len = s.LastIndexOf(".") - s.LastIndexOf("\\") - 1;
                            string fileName = DateTime.Now.ToString("yyyyMMdd") + DateTime.Now.Millisecond.ToString();
                            string fullName = fileName + "." + strFileExtension;

                            string path = dir + "\\" + fullName;
                            phPath = physicsPath + "//" + fullName;
                            string pp = path.Replace("\\", "/");
                            //保存文件  
                            var igore=context.Request["ignore"];
                            //img.InputStream
                            if (fileType != "file")
                            {
                                if ("gif" == igore && strFileExtension.ToLower() == "gif")
                                {

                                    img.SaveAs(phPath);


                                }
                                else
                                {
                                    int width = int.Parse(context.Request["width"]);
                                    int fileLen = img.ContentLength;
                                    // 读取文件的 byte[] 
                                    byte[] bytes = new byte[fileLen];

                                    img.InputStream.Read(bytes, 0, fileLen);
                                    MemoryStream ms = new MemoryStream(bytes);

                                    ImageClass.MakeThumbnail(ms, phPath, width, 0, "W");

                                    ms.Close();
                                    ms.Dispose();
                                }
                            }
                            else
                            {
                                img.SaveAs(phPath);
                            }


                            context.Response.Write(JsonConvert.SerializeObject(new { error = 0, url = pp }));
                        }
                        else
                        {
                            context.Response.Write(JsonConvert.SerializeObject(new { error = 1, message = "文件格式不支持" }));
                        }
                    }
                    else
                    {
                        context.Response.Write(JsonConvert.SerializeObject(new { error = 1, message = "网络错误,后台没接收到文件" }));
                    }
                }
                catch (Exception)
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { error = 1, message = "上传图片出错" }));
                    throw;
                }
            }

            else if (string.Compare(action, "noticeAE", false) == 0)
            {


                var id = context.Request["noticeId"];
                var title = context.Request["noticeTitle"];
                var content = context.Request["noticeContent"];
                var time = context.Request["DataStart"];
                DateTime time1 = DateTime.Now;
                if (!string.IsNullOrEmpty(time))
                {
                    time1 = Convert.ToDateTime(time);
                }
                Notice notice = new Notice();
                string sql = "";
                //编辑
                if (!string.IsNullOrEmpty(id))
                {
                    notice.Id = int.Parse(id);
                    notice.NoticeContent = content.Replace("'", "");
                    notice.Title = title;
                    notice.NoticeTime = time;
                    sql = SqlBuilderHelper.UpdateSql<Notice>(notice, "Notice");
                }
                else
                {//新增

                    notice.NoticeContent = content.Replace("'", "");
                    notice.Title = title;
                    notice.NoticeTime = time;

                    sql = SqlBuilderHelper.InsertSql<Notice>(notice, "Notice");

                }

                int result = -1;
                if (sql != "")
                {
                    result = helper.GetNonQuery(sql);
                }

                if (result != -1)
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "Ok", msg = "操作成功" }));
                   

                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "NoOk", msg = "操作失败" }));

                }

            }
            else if (string.Compare(action, "ArticleAE", false) == 0)
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);

                if (ticket == null)
                {

                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "NoOk", msg = "请刷新页面再试" }));
                    context.Response.End();
                }


                LLArticle article = new LLArticle();

                int artId = String.IsNullOrEmpty(context.Request["ArtId"]) ? -1 : int.Parse(context.Request["ArtId"]);
                string listStr = context.Request["ArtType"];

                if ("" != listStr && null != listStr)
                {
                    article.ArtType = listStr.Split(new char[] { ',' }).Select(str => int.Parse(str)).ToList();
                }



                article.Content = context.Request["Content"].Replace("'", "");
                article.Title = context.Request["ArtTitle"];
                article.Digest = context.Request["Digest"];
                article.ReadPwd = context.Request["ArtPwd"];
                article.HomeImg = context.Request["HomeImg"];
                bool flag = false;
                if(context.Request["IsTop"]=="1")
                {
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                article.IsTop = flag;
                string ti = context.Request["ArtCreateTime"];
                if (string.IsNullOrEmpty(ti) || ti == "")
                    article.ArtTime = DateTime.Now;
                else

                article.ArtTime = DateTime.Parse(ti);
                article.Editor = ticket.Name;
                string sql = "";
                if (artId > 0)
                {
                    var strSql = "select PresentCount,CommentCount,ReadCount from LLArticle where Id=" + artId;
                    var tb = helper.GetDataTable(strSql, null);

                    for (int t = 0; t < tb.Rows.Count; t++)
                    {
                        article.PresentCount = Convert.ToInt32(tb.Rows[t][0]);
                        article.CommentCount = Convert.ToInt32(tb.Rows[t][1]);
                        article.ReadCount = Convert.ToInt32(tb.Rows[t][2]);
                    }


                    article.LastUpdatetime = DateTime.Now;
                    article.Id = artId;
                    sql = SqlBuilderHelper.UpdateSql<LLArticle>(article, "LLArticle");
                }
                else
                {
                    article.ArtTime = DateTime.Now;
                    article.LastUpdatetime = new DateTime(1900, 1, 1);
                    
                    sql = SqlBuilderHelper.InsertSql<LLArticle>(article, "LLArticle");

                    sql += ";select @@Identity";
                }

               
                int result = -1;
                if (sql != "")
                {
                    StringBuilder sb = new StringBuilder();
                    if (artId > 0) {

                        string deleteSql = "delete from [ArtiAndType] where ArtId=" + artId;
                        helper.GetNonQuery(deleteSql);

                        result = helper.GetNonQuery(sql);
                    }
                    else
                    {
                        result = Convert.ToInt32(helper.GetScalar(sql, null));
                    }
                    string format = "insert into ArtiAndType([ArtId],[TypeId])values({0},{1});\r\n";

                    //添加文章所属类别
                    if (null != article.ArtType && article.ArtType.Count > 0)
                    {
                        foreach (var item in article.ArtType)
                        {
                            sb.Append(string.Format(format, artId > 0 ? artId : result, item));
                        }

                        result = helper.GetNonQuery(sb.ToString());
                    }
                }

                if (result >0)
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "Ok", msg = "操作成功" }));
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "NoOk", msg = "操作失败" }));

                }
            }
            else if (string.Compare(action, "TimeLineAE", false) == 0)
            {
                LLTime timeLine = new LLTime();

                var id = context.Request["TimeLineId"];
                var title = context.Request["TimeLineTitle"];
                var content = context.Request["TimeLineContent"];
                var time = context.Request["TimeLineTime"];
                var link = context.Request["TimeLineLink"];
                DateTime time1 = DateTime.Now;
                timeLine.Link = link;
                timeLine.TimeContent = content;
                timeLine.TimeTitle = title;

                if (!string.IsNullOrEmpty(time))
                {
                    time1 = Convert.ToDateTime(time);
                }
                timeLine.CreateTime = time1;
                string sql = string.Empty;
                //编辑
                if (!string.IsNullOrEmpty(id))
                {
                    timeLine.Id = int.Parse(id);
                    sql = SqlBuilderHelper.UpdateSql<LLTime>(timeLine, "LLTime");
                }
                else
                {//新增
                    sql = SqlBuilderHelper.InsertSql<LLTime>(timeLine, "LLTime");
                }
                int re = helper.GetNonQuery(sql);

                if (re > 0)
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "Ok", msg = "操作完成" }));
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "NoOk", msg = "操作失败" }));
                }

            }         
            else if (string.Compare(action, "artTypeAE", false) == 0)
            {


                var id = context.Request["TypeId"];
                var title = context.Request["TypeTitle"];
                var detail = context.Request["TypeDetail"];
                var time = context.Request["TypeCTime"];
                var isHidden = context.Request["TypeIsHidden"];
                DateTime time1 = DateTime.Now;
                if (!string.IsNullOrEmpty(time))
                {
                    time1 = Convert.ToDateTime(time);
                }

                LLType llType = new LLType();

                llType.TypeName = title;
                llType.IsHidden = int.Parse(isHidden);
                llType.CreateTime = time1;
                llType.TypeDetail = detail;


                string sql = "";
                //编辑
                if (!string.IsNullOrWhiteSpace(id))
                {
                    llType.Id = int.Parse(id);

                    sql = SqlBuilderHelper.UpdateSql<LLType>(llType, "[LLArtType]");

                }
                else
                {//新增
                    sql = SqlBuilderHelper.InsertSql<LLType>(llType, "LLArtType");
                }
                int re = helper.GetNonQuery(sql);


                if (re > 0)
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "Ok", msg = "操作完成" }));
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "NoOk", msg = "操作失败" }));
                }
            }           
            else if (string.Compare(action, "bannerAE", false) == 0)
            {
                string banAddr = context.Request["BanAddr"];
                string banImgAddr = context.Request["BanImgAddr"];
                string banSort = context.Request["BanSort"];
                string banId = context.Request["BanId"];
                string banTitle = context.Request["BanTitle"];
                int IsBanShow = int.Parse(context.Request["BanIsShow"]);

                Banner banner = new Banner();

                banner.BanAddr = banAddr;
                banner.BanImgAddr = banImgAddr;
                banner.BanIsShow = IsBanShow;
                banner.BanSort = int.Parse(banSort);
                banner.BanTitle=banTitle;
                string sql = string.Empty;

                if (string.IsNullOrEmpty(banId))
                {
                    //新增
                    sql = SqlBuilderHelper.InsertSql<Banner>(banner, "Banner");
                }
                else
                {
                    //编辑
                    banner.Id = int.Parse(banId);
                    sql = SqlBuilderHelper.UpdateSql<Banner>(banner, "Banner");
                }
               int re= helper.GetNonQuery(sql);

                if (re > 0)
                {

                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "Ok", msg = "操作完成" }));
                }
                else
                {
                    context.Response.Write(JsonConvert.SerializeObject(new { IsOk = "NoOk", msg = "操作失败" }));
                }
            }
            else if (string.Compare(action, "llDelete", false) == 0)
            {
                string table = context.Request["table"];
                string id = context.Request["Id"];
                string field = context.Request["field"];
                if (string.IsNullOrWhiteSpace(field))
                {
                    field = "id";
                }


                string sql = "delete from {0} where {1}={2}";

                sql = string.Format(sql, table, field,id);

                helper.GetNonQuery(sql);


            }

            else if (string.Compare(action, "llDeleteIds", false) == 0)
            {
                string table = context.Request["table"];
                string ids = context.Request["Ids"];
                string field = context.Request["field"];
                if (string.IsNullOrWhiteSpace(field))
                {
                    field = "id";
                }


                string sql = "delete from {0} where {1} in {2}";

                sql = string.Format(sql, table, field, ids);

                helper.GetNonQuery(sql);


            }
            


        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}